﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using System.Collections.Generic;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Entities;
using Sio.Mdm.Types.Database;
using Sio.Mdm.BF.Administracija;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.Web.Controls;


namespace Sio.Mdm.Web.Organizacija
{
    public partial class NovKader : Controls.Page
    {
        #region OnLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                //delovna mesta
                EntityCollection<DelovnoMesto> delMesto = new EntityCollection<DelovnoMesto>(
                    new Filter<DelovnoMesto>().Where(r => r.DelovnoMestoId != null).OrderBy(r => r.Naziv));
                this.StoreDelovnaMesta.DataSource = delMesto;
                this.StoreDelovnaMesta.DataBind();

                //poklici
                EntityCollection<Poklic> poklici = new EntityCollection<Poklic>(
                    new Filter<Poklic>().Where(r => r.PoklicId != null).OrderBy(r => r.Naziv));
                this.StorePoklici.DataSource = poklici;
                this.StorePoklici.DataBind();

                int tipVloge = Request.GetValue<int>("tipVloge");
                if (tipVloge == 4) //pedagoški
                {
                    //nazivi učiteljev
                    EntityCollection<NazivUcitelja> nazivi = new EntityCollection<NazivUcitelja>(
                        new Filter<NazivUcitelja>().Where(r => r.NazivUciteljaId != null).OrderBy(r => r.Naziv));
                    this.StoreNaziviUciteljev.DataSource = nazivi;
                    this.StoreNaziviUciteljev.DataBind();
                }
                else this.cbNaziviUciteljev.Visible = false;
            }
        }
        #endregion

        protected void AddKaderButton(object sender, DirectEventArgs e)
        {
            Sio.Mdm.Web.Controls.DialogButtons button =
                e.DeserializeParameter<Sio.Mdm.Web.Controls.DialogButtons>("buttonType");

            if (button == DialogButtons.Save)
            {
                int tipVloge = Request.GetValue<int>("tipVloge");
                if ((tipVloge == 4) || (tipVloge == 5))
                {
                    if (tipVloge == 4) //pedagoški
                    {
                        KaderPedagoski kader = EntityFactory.CreateInstance<KaderPedagoski>();
                        kader.Ime = this.inputIme.Value.ToString();
                        kader.Priimek = this.inputPriimek.Value.ToString();
                        kader.Spol = Convert.ToInt32(this.cbSpol.SelectedItem.Value);
                        kader.DavcnaStevilka = Convert.ToInt32(this.inputDs.Value);
                        if (this.cbDelovnaMesta.SelectedItem.Value != "")
                            kader.DelovnoMestoId = Convert.ToInt32(this.cbDelovnaMesta.SelectedItem.Value);
                        if (this.cbPoklic.SelectedItem.Value != "")
                            kader.PoklicId = Convert.ToInt32(this.cbPoklic.SelectedItem.Value);
                        kader.OrganizacijaId = this.UserSession.Organizacija;
                        kader.TipVlogeId = tipVloge;
                        if (this.cbNaziviUciteljev.SelectedItem.Value != "")
                            kader.NazivUciteljaId = Convert.ToInt32(this.cbNaziviUciteljev.SelectedItem.Value);
                        var slService = ServiceFactory.GetService<IKaderPedagoskiService>();
                        slService.Create(kader, this.UserSession);
                    }
                    else //nepedagoški
                    {
                        KaderNePedagoski kader = EntityFactory.CreateInstance<KaderNePedagoski>();
                        kader.Ime = this.inputIme.Value.ToString();
                        kader.Priimek = this.inputPriimek.Value.ToString();
                        kader.Spol = Convert.ToInt32(this.cbSpol.SelectedItem.Value);
                        kader.DavcnaStevilka = Convert.ToInt32(this.inputDs.Value);
                        if (this.cbDelovnaMesta.SelectedItem.Value != "")
                            kader.DelovnoMestoId = Convert.ToInt32(this.cbDelovnaMesta.SelectedItem.Value);
                        if (this.cbPoklic.SelectedItem.Value != "")
                        kader.PoklicId = Convert.ToInt32(this.cbPoklic.SelectedItem.Value);
                        kader.OrganizacijaId = this.UserSession.Organizacija;
                        kader.TipVlogeId = tipVloge;
                        //kader.NazivUciteljaId = Convert.ToInt32(this.cbNaziviUciteljev.SelectedItem.Value);
                        var slService = ServiceFactory.GetService<IKaderNepedagoskiService>();
                        slService.Create(kader, this.UserSession);

                    }
                }

                AddKaderProxy.Close();
            }
            if (button == Sio.Mdm.Web.Controls.DialogButtons.Close)
            {
                AddKaderProxy.Close();
            }
        }


    }
}
